//
//  LadingBillListCell.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/1.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "LadingBillListCell.h"
#import "LadingBillMainModel.h"

@interface LadingBillListCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *noTitleLab;
@property (nonatomic, strong) UILabel *noLab;
@property (nonatomic, strong) UIButton *fuZhiBtn;
@property (nonatomic, strong) UIButton *operationBtn;
@property (nonatomic, strong) UILabel *fromAddLab;
@property (nonatomic, strong) UIImageView *statusImgView;
@property (nonatomic, strong) UILabel *statusLab;
@property (nonatomic, strong) UILabel *toAddLab;
@property (nonatomic, strong) UILabel *companyTitleLab;
@property (nonatomic, strong) UILabel *companyLab;
@property (nonatomic, strong) UILabel *timeTitleLab;
@property (nonatomic, strong) UILabel *timeLab;

@end

@implementation LadingBillListCell

+ (instancetype)LadingBillListCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"LadingBillListCell";
    LadingBillListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[LadingBillListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = Color_backgroundColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bgView = [UIView new];
        self.bgView.backgroundColor = HexColor(0xffffff);
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 6;
        [self.contentView addSubview:self.bgView];
        
        self.noTitleLab = [UILabel new];
        self.noTitleLab.textColor = HexColor(0x9C9DB3);
        self.noTitleLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.noTitleLab];
        
        self.noLab = [UILabel new];
        self.noLab.textColor = HexColor(0x2B2C44);
        self.noLab.font = BoldFontOfSize(12);
        [self.bgView addSubview:self.noLab];
        
        self.fuZhiBtn = [UIButton new];
        [self.bgView addSubview:self.fuZhiBtn];
        
        self.operationBtn = [UIButton new];
        self.operationBtn.titleLabel.font = BoldFontOfSize(14);
        [self.operationBtn addTarget:self action:@selector(operationClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:self.operationBtn];
        
        self.fromAddLab = [UILabel new];
        self.fromAddLab.textColor = HexColor(0x2B2C44);
        self.fromAddLab.font = BoldFontOfSize(20);
        self.fromAddLab.textAlignment = NSTextAlignmentCenter;
        [self.bgView addSubview:self.fromAddLab];
        
        self.statusImgView = [UIImageView new];
        [self.bgView addSubview:self.statusImgView];
        
        self.statusLab = [UILabel new];
        self.statusLab.textColor = HexColor(0x2B2C44);
        self.statusLab.font = BoldFontOfSize(12);
        [self.bgView addSubview:self.statusLab];
        
        self.toAddLab = [UILabel new];
        self.toAddLab.textColor = HexColor(0x2B2C44);
        self.toAddLab.font = BoldFontOfSize(20);
        self.toAddLab.textAlignment = NSTextAlignmentCenter;
        [self.bgView addSubview:self.toAddLab];
        
        self.companyTitleLab = [UILabel new];
        self.companyTitleLab.textColor = HexColor(0x9C9DB3);
        self.companyTitleLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.companyTitleLab];
        
        self.companyLab = [UILabel new];
        self.companyLab.textColor = HexColor(0x2B2C44);
        self.companyLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.companyLab];
        
        self.timeTitleLab = [UILabel new];
        self.timeTitleLab.textColor = HexColor(0x9C9DB3);
        self.timeTitleLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.timeTitleLab];
        
        self.timeLab = [UILabel new];
        self.timeLab.textColor = HexColor(0x2B2C44);
        self.timeLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.timeLab];
        
    }
    return self;
}

-(void)setBillModel:(LadingBillMainModel *)billModel {
    _billModel = billModel;
    [self configUI];
}

- (void)operationClick {
    if (self.operationBlock) {
        self.operationBlock();
    }
}

- (void)configUI {
    self.bgView.frame = CGRectMake(15, 10, ScreenW - 30, 180);
    
    self.noTitleLab.text = @"提货单号：";
    self.noTitleLab.nim_left = 10;
    self.noTitleLab.nim_top = 15;
    [self.noTitleLab sizeToFit];
    
    self.noLab.text = self.billModel.serial_num;
    self.noLab.nim_left = self.noTitleLab.nim_right;
    [self.noLab sizeToFit];
    self.noLab.nim_centerY = self.noTitleLab.nim_centerY;
    
    self.fuZhiBtn.nim_left = self.noLab.nim_right + 6;
    self.fuZhiBtn.nim_width = 18;
    self.fuZhiBtn.nim_height = 18;
    self.fuZhiBtn.nim_centerY = self.noLab.nim_centerY;
    [self.fuZhiBtn setImage:[UIImage imageNamed:@"icon_ladingbill_erweima"] forState:0];
    
    self.operationBtn.nim_width = 60;
    self.operationBtn.nim_height = 30;
    self.operationBtn.nim_left = self.bgView.nim_width - 10 - self.operationBtn.nim_width;
    self.operationBtn.nim_centerY = self.noTitleLab.nim_centerY;
    self.operationBtn.layer.masksToBounds = YES;
    self.operationBtn.layer.cornerRadius = 15;
    if (self.billModel.status == LadingBillStatusModeNew) {
        if (self.billModel.dp_type == 1) {
            [self.operationBtn setTitle:@"确认" forState:0];
            [self.operationBtn setTitleColor:HexColor(0xFBA962) forState:0];
            self.operationBtn.backgroundColor = HexColorA(0xFBA962, 0.1);
        }else if (self.billModel.dp_type == 2) {
            [self.operationBtn setTitle:@"删除" forState:0];
            [self.operationBtn setTitleColor:HexColor(0xF35959) forState:0];
            self.operationBtn.backgroundColor = HexColorA(0xF35959, 0.1);
        }
    }else {
        self.operationBtn.frame = CGRectZero;
    }
    
    self.statusImgView.nim_width = 81;
    self.statusImgView.nim_height = 10;
    self.statusImgView.nim_left = (self.bgView.nim_width - 81) * 0.5;
    self.statusImgView.nim_top = self.fuZhiBtn.nim_bottom + 33;
    if (self.billModel.status == LadingBillStatusModeNew) {
        [self.statusImgView setImage:[UIImage imageNamed:@"ladingBill_new"]];
    }else if (self.billModel.status == LadingBillStatusModeSure) {
        [self.statusImgView setImage:[UIImage imageNamed:@"ladingBill_sure"]];
    }else if (self.billModel.status == LadingBillStatusModeDiaoDu) {
        [self.statusImgView setImage:[UIImage imageNamed:@"ladingBill_diaoDu"]];
    }else if (self.billModel.status == LadingBillStatusModeChuFa) {
        [self.statusImgView setImage:[UIImage imageNamed:@"ladingBill_chuFa"]];
    }else if (self.billModel.status == LadingBillStatusModeDaoDa) {
        [self.statusImgView setImage:[UIImage imageNamed:@"ladingBill_daoDa"]];
    }else if (self.billModel.status == LadingBillStatusModeZhuangChe) {
        [self.statusImgView setImage:[UIImage imageNamed:@"ladingBill_zhuangChe"]];
    }else if (self.billModel.status == LadingBillStatusModeBoHui) {
        [self.statusImgView setImage:[UIImage imageNamed:@"ladingBill_boHui"]];
    }else if (self.billModel.status == LadingBillStatusModeFinish) {
        [self.statusImgView setImage:[UIImage imageNamed:@"ladingBill_finish"]];
    }else if (self.billModel.status == LadingBillStatusModeZuoFei) {
        [self.statusImgView setImage:[UIImage imageNamed:@"ladingBill_zuoFei"]];
    }
    
    self.statusLab.text = self.billModel.status_name;
    [self.statusLab sizeToFit];
    self.statusLab.nim_centerX = self.statusImgView.nim_centerX;
    self.statusLab.nim_top = self.statusImgView.nim_bottom + 10;
    
    self.fromAddLab.nim_left = 10;
    self.fromAddLab.text = self.billModel.start_city;
    [self.fromAddLab sizeToFit];
    self.fromAddLab.nim_width = self.statusImgView.nim_left - 20;
    self.fromAddLab.nim_centerY = self.statusImgView.nim_centerY;
    
    self.toAddLab.nim_left = 10+self.statusImgView.nim_right;
    self.toAddLab.text = self.billModel.end_city;
    [self.toAddLab sizeToFit];
    self.toAddLab.nim_width = self.statusImgView.nim_left - 20;
    self.toAddLab.nim_centerY = self.statusImgView.nim_centerY;
    
    self.timeTitleLab.text = @"提货时间：";
    [self.timeTitleLab sizeToFit];
    self.timeTitleLab.nim_left = 10;
    self.timeTitleLab.nim_top = self.bgView.nim_height - 15 - self.timeTitleLab.nim_height;
    
    self.timeLab.text = [NSString stringWithFormat:@"%@ — %@",self.billModel.start_time,self.billModel.end_time];
    [self.timeLab sizeToFit];
    self.timeLab.nim_left = self.timeTitleLab.nim_right;
    self.timeLab.nim_centerY = self.timeTitleLab.nim_centerY;
    self.timeLab.nim_width = self.bgView.nim_width - 10 - self.timeTitleLab.nim_right;
    
    self.companyTitleLab.text = @"承运方：";
    [self.companyTitleLab sizeToFit];
    self.companyTitleLab.nim_left = self.timeTitleLab.nim_left;
    self.companyTitleLab.nim_top = self.timeTitleLab.nim_top - 5 - self.companyTitleLab.nim_height;
    
    self.companyLab.text = self.billModel.name;
    [self.companyLab sizeToFit];
    self.companyLab.nim_left = self.companyTitleLab.nim_right;
    self.companyLab.nim_centerY = self.companyTitleLab.nim_centerY;
    self.companyLab.nim_width = self.bgView.nim_width - 10 - self.companyTitleLab.nim_right;
    
    
    self.height = self.bgView.nim_bottom;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
